

const antenna = {
  state: {
    antennadata: [],
    namemap: {
      satellite: [],
      beam: []
    },
    submitdata: {},
    updata:{}
  },
  mutations: {
    setantennadata(state, data) {
      state.antennadata = data || []
    },

    setNameMap(state, data) {
      console.log(data);
      state.namemap.satellite = data.satellite || []
      state.namemap.beam = data.beam || []
      console.log(state.namemap.satellite);
      console.log( state.namemap.beam);
    },
    setAddsubmitdata(state, data) {
      state.submitdata = data
      console.log(data);
    },
    updataantennadata(state,data){
      state.updata = data
    }
  },
  actions: {
    async getantennas(context, payload) {
      let response = await context.dispatch('axios', {
        method: 'get',
        url: '/api/sam/web/antenna',

      })
      context.commit('setantennadata', response.data.antennas)
    },
    async getsatellite_name(context, payload) {
      let response = await context.dispatch('axios', {
        method: 'get',
        url: '/api/sam/web/namemap',

      })
      context.commit('setNameMap', response.data)
    },
    async addantennas(context, payload) {
      let response = await context.dispatch('axios', {
        method:'post',
        url:'/api/sam/web/antenna',
        params:{
          payload
        }
      })
    },
    async updataantennas(context,payload){
      // 更改数据的地方   2021年1月8号
      let response = await context.dispatch('axios',{
        method:'put',
        url:'/api/sam/web/antenna',
        params:{
          payload
        }
      })
    },
    async deleteantennas(context,payload){
      let response = await context.dispatch('axios',{
        method:'delete',
        url:'/api/sam/web/antenna',
        params:{
          id:payload
        }
      })
    }
  },
  getters: {
      antennaDataTableList(state) {
      return state.antennadata.map(antennaItem => {
        console.log(state.namemap.satellite);
        let satellite = state.namemap.satellite.find(satelliteItem => satelliteItem.id === antennaItem.satellite_id)
        let beam = state.namemap.beam.find(beamItem => beamItem.id === antennaItem.beam_id)
        return {
          ...antennaItem,
          satellite_name: satellite && satellite.name,
          beam_name: beam && beam.name
        }
      })
    },
    antennaSubmitDate(state) {
      console.log(state.submitdata);
      for (const submitdataItem in state.submitdata) {
        var satellite_id = state.namemap.satellite.find(satelliteItem => satelliteItem.name === state.submitdata[submitdataItem]);
        if(satellite_id != {}){
          var beam_id= state.namemap.beam.find(beamItem => beamItem.satellite_id === satellite_id.id);
          state.submitdata.satellite_id = satellite_id.id
          state.submitdata.beam_id =beam_id.id
         return  state.submitdata
        }
          }
    },
  }



}
export default antenna